<?php

if (!defined('NOT_HTMLPAGE'))
{
	define('NOT_HTMLPAGE', 'json');
}
define('NOT_LOGIN', TRUE);
define('IS_MEMBER', TRUE);
require dirname(__FILE__).'/../include/common.inc.php';
global_LoadLang('api');

$webglobal['sitetimemode'] = 'Y F d l ga';
$webglobal['weddingdatemode'] = 'jS F Y';
$webglobal['lastedittime'] = 172800;	//48 hour
$webglobal['loginintime'] = 2592000;	//30 day

$return = array('code'=>0,'msg'=>'','data'=>array());

if (isset($webrequest['param']) && substr($webrequest['param'],0,1)=='{' && substr($webrequest['param'],-1)=='}')
{
	$apiparam = global_Addslashes(@json_decode(stripslashes($webrequest['param']), TRUE), 1);
	$webrequest += $apiparam;
}

if (defined('IS_MEMBER_CHECK') && IS_MEMBER_CHECK)
{
	if (!$user['m_islogin'])
	{
		$return['code'] = 10000;
		$return['msg'] = $weblang['api_notLogin'];
		api_EchoReturn();
	}
}

function api_EchoReturn($strmsg = '')
{
	global $return;
	if ($strmsg != '')
	{
		$return['code'] = 1;
		$return['msg'] = $strmsg;
	}
	if (empty($return['data']))
	{
		unset($return['data']);
	}
	if (NOT_HTMLPAGE == 'json')
	{
		echo json_encode($return);
	}
	elseif (NOT_HTMLPAGE == 'html')
	{
		echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /><style type="text/css">html,body,td{text-align:center;margin:10px;font-family:Arial;font-size:14px;word-wrap:break-word;background-color:transparent;}</style></head><body>'.$return['msg'].'</body></html>';
	}
	exit();
}

function api_GetTopActivityID($intErr = 1)
{
	global $db, $weblang;
	$tempdata = $db->GetOne("SELECT id FROM promotion ORDER BY id DESC LIMIT 0,1");
	if (empty($tempdata))
	{
		if ($intErr)
		{
			api_EchoReturn($weblang['api_notHaveGift']);
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return $tempdata['id'];
	}
}

function api_FormatContent($strcontent = '', $intcodetime = 0)
{
	$return = array('','');
	$temppagebreaktag = '_page_break_tag_';
	$temphtmlheader = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /><style type="text/css">*{font-family:Arial;font-size:14px;word-wrap:break-word;background-color:transparent;}</style></head><body>';
	$temphtmlfooter = '</body></html>';
	$templimitdate = $intcodetime>0 ? global_FormatTime($intcodetime+30*86400, 'Y/m/d') : '4 weeks';
	$strcontent = str_replace('#limitdate#', $templimitdate, $strcontent);
	$tempcontentarray = explode($temppagebreaktag, $strcontent);
	$tempcontentarraynum = count($tempcontentarray);
	if ($tempcontentarraynum == 1)
	{
		$return[0] = '';
		$return[1] = $temphtmlheader.(isset($tempcontentarray[0]) ? trim($tempcontentarray[0]) : '').$temphtmlfooter;
	}
	else
	{
		$return[0] = $temphtmlheader.(isset($tempcontentarray[0]) ? trim($tempcontentarray[0]) : '').$temphtmlfooter;
		$return[1] = $temphtmlheader.(isset($tempcontentarray[1]) ? trim($tempcontentarray[1]) : '').$temphtmlfooter;
	}
	return $return;
}
